import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from '../views/Register'
import Login from '../views/Login.vue'
import Userinfo from '../views/Userinfo.vue'
import Edit from '../views/Edit.vue'
import Home from '../views/Home.vue'
import Article from '../views/Article.vue'
import Search from '../components/serach/search.vue'
import Editcategory from '../views/Editcategory.vue'

Vue.use(VueRouter)

const router = new VueRouter({
	// mode: 'history',
	// base: process.env.BASE_URL,
	routes: [
		{path: '/',component: Home,meta:{keepalive:true}},
		{path: '/register',component: Register},
		{path: '/login',component: Login},
		{path: '/userinfo',component: Userinfo,
		meta:{
			istoken:true,
			},
		},
		{path: '/edit',component: Edit},
		{path:'/article/:id',component:Article},
		{path:'/search',component:Search},
		{path:'/editcategory',component:Editcategory}
	]
})


// 这个方法是在每次路由跳转之前打印每个路由的信息,因为路由跳转的时候不会发送ajax,所以要用router判断(拦截)
router.beforeEach((to,from,next)=>{
	if(!localStorage.getItem('token')&&!localStorage.getItem('token')&&to.meta.istoken==true){
		router.push('/login')
		return
	}else{
		next()
	}
})



export default router
